local yquan = fk.CreateSkill {
  name = "yquan",
  anim_type = "control",
}

Fk:loadTranslationTable {
  ["yquan"] = "御犬",
  [":yquan"] = "出牌阶段限一次，你可以弃置场上一张牌并选择一项：①坐下：令一名角色将一张手牌置于其武将牌上，称为“狗”；②握手：与一名其他角色交换体力上限，若其武将牌上有“狗”，你将恢复体力至上限；③扑咬：选择一名有“狗”的角色对其攻击范围的一名角色造成1点伤害。",
  ["yquanoption1"] = "坐下：令一名角色将一张手牌置于其武将牌上，称为“狗”",
  ["yquanoption2"] = "握手：与一名角色交换体力上限，若其有“狗”，你回复体力至上限",
  ["yquanoption3"] = "扑咬：选择一名有狗牌的角色对其攻击范围内另一角色造成一点伤害",
  ["#yquan-card"] = "御犬：请选择场上的一张牌弃置，然后执行你选择的这一项",
  ["#yquan-sit"] = "御犬·坐下：选择一名角色，令其将一张手牌置为“狗”",
  ["#yquan-sit2"] = "御犬·坐下：请将一张手牌置为“狗”",
  ["#yquan-hand"] = "御犬·握手：选择一名角色，与其交换体力上限",
  ["#yquan-bite"] = "御犬·扑咬：选择一名有“狗”的角色，然后选择其攻击范围内一名角色造成1点伤害",
  ["#yquan-bite-target"] = "御犬·扑咬：选择一名角色对其造成1点伤害",
  ["dog"] = "狗",
}

yquan:addEffect("active", {
  prompt = "#yquan-card",
  min_target_num = 1,
  max_target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(yquan.name, Player.HistoryPhase) == 0
  end,
  target_filter = function(self, player, to_select, selected, cards)
    return to_select and #to_select:getCardIds("ej") > 0
  end,
  interaction = function(self, player)
    return UI.ComboBox { choices = { "yquanoption1", "yquanoption2", "yquanoption3" } }
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local to_discard = room:askToChooseCard(player, {
      target = effect.tos[1],
      flag = "ej",
      skill_name = yquan.name,
    })
    room:throwCard(to_discard, yquan.name, effect.tos[1], player)

    if self.interaction.data == "yquanoption1" then
      -- 坐下：选择一名角色放置"狗牌"
      local targets = table.filter(room:getAlivePlayers(), function(p)
        return not p:isKongcheng()
      end)
      local target = room:askToChoosePlayers(player, {
        min_num = 1,
        targets = targets,
        skill_name = yquan.name,
        max_num = 1,
        cancelable = false,
        prompt = "#yquan-sit"
      })
      if #target > 0 then
        local targetPlayer = target[1]
        local card_id = room:askToCards(targetPlayer, {
          min_num = 1,
          max_num = 1,
          pattern = ".",
          cancelable = false,
          include_equip = false,
          prompt = "#yquan-sit2",
          skill_name = yquan.name
        })
        room:moveCards({
          ids = card_id,
          from = targetPlayer,
          to = targetPlayer,
          toArea = Card.PlayerSpecial,
          moveReason = fk.ReasonPut,
          specialName = "dog",
          proposer = targetPlayer,
          skillName = yquan.name,
        })
      end
    elseif self.interaction.data == "yquanoption2" then
      -- 握手：与一名角色交换体力值
      local target = room:askToChoosePlayers(player, {
        min_num = 1,
        targets = room:getOtherPlayers(player),
        skill_name = yquan.name,
        max_num = 1,
        cancelable = false,
        prompt = "#yquan-hand",
      })

      if #target > 0 then
        local player_maxhp = player.maxHp
        local target_maxhp = target[1].maxHp
        room:setPlayerProperty(target[1], "maxHp", player_maxhp)
        if target[1].hp > target[1].maxHp then
          room:setPlayerProperty(target[1], "hp", target[1].maxHp)
        end
        room:setPlayerProperty(player, "maxHp", target_maxhp)
        if player.hp > player.maxHp then
          room:setPlayerProperty(player, "hp", player.maxHp)
        end

        -- 若目标有"狗牌"，则恢复体力至上限
        if #target[1]:getPile("dog") > 0 then
          room:recover({
            who = player,
            num = player.maxHp - player.hp,
            recoverBy = player,
            skillName = yquan.name,
          })
        end
      end
    else
      -- 扑咬：有狗牌角色对其攻击范围内角色造成伤害
      local targets = table.filter(room:getAlivePlayers(), function(p)
        return #p:getPile("dog") > 0
      end)
      if #targets > 0 then
        local dogPlayer = room:askToChoosePlayers(player, {
          min_num = 1,
          targets = targets,
          skill_name = yquan.name,
          max_num = 1,
          cancelable = false,
          prompt = "#yquan-bite",
        })[1]
        local targets2 = table.filter(room.alive_players, function(p)
          return dogPlayer:inMyAttackRange(p)
        end)
        local target2 = room:askToChoosePlayers(player, {
          min_num = 1,
          targets = targets2,
          skill_name = yquan.name,
          max_num = 1,
          cancelable = false,
          prompt = "#yquan-bite-target",
        })[1]
        room:damage {
          from = player,
          to = target2,
          damage = 1,
          skillName = yquan.name,
        }
      end
    end
  end,
})

return yquan
